<?php

/**
 * 
 * @project     : innoTweets 1.0
 * @author      : Dino (DBK)
 * 
 * @package     : User gained likes count library
 * 
 */
if (!defined('BASEPATH')) {
  exit('No direct script access allowed');
}

class User_reputation {

  private $ci;
  private $tweet_weightage = 15;
  private $tweet_reply_weightage = 10;
  private $comment_weightage = 10;
  private $album_weightage = 5;
  private $photo_weightage = 5;
  private $poll_weightage = 5;
  private $tweet_point = 2;
  private $reply_point = 1;
  private $comment_point = 1;
  private $album_point = 2;
  private $poll_point = 2;

  public function __construct() {
    $this->ci = & get_instance();
  }

  // Reterive all likes count on innoTweets
  // Calculate the percentage with the given user id's gained likes
  // Returns the user's likes level :)
  public function init($user_id = NULL) {
    if (!empty($user_id) && is_numeric($user_id)) {
      return $user_likes_count = (int) $this->calculate($user_id);
      $get_all_likes_by_type = $this->ci->like_model->get_all_likes_by_type();
      $all_likes_count = $get_all_likes_by_type['count'];
      return @round(($user_likes_count / $all_likes_count) * 100);
    }
    return 0;
  }

  // Takes all the tweets, replies, albums, polls, comments, photos added by the given user id.
  // Then takes the likes gained by each of them.
  // Calculate the total likes gained by the user.
  private function calculate($user_id = NULL) {
    $total_likes_count = 0;
    $get_tweets = $this->ci->tweet_model->get_my_tweets($user_id, NULL);
    $tweets = $get_tweets['result'];
    $get_my_replies = $this->ci->tweet_model->get_my_replies($user_id, NULL);
    $replies = $get_my_replies['result'];
    $get_albums = $this->ci->album_model->get_albums($user_id, NULL);
    $albums = $get_albums['result'];
    $get_polls = $this->ci->poll_model->get_my_polls(NULL, $user_id);
    $polls = $get_polls['result'];
    $get_my_comments = $this->ci->comment_model->get_my_comments($user_id, NULL);
    $comments = $get_my_comments['result'];
    $get_my_photos = $this->ci->album_model->get_my_photos($user_id);
    $photos = $get_my_photos['result'];

    $tweets_like_count = 0;
    if (!empty($tweets) && is_array($tweets)) {
      foreach ($tweets as $tweet) {
        $tweets_likes = get_user_like_status('tweet', $tweet['tweet_id']);
        if (!empty($tweets_likes['total_count'])) {
          $tweets_like_count = $tweets_like_count + $tweets_likes['total_count'];
        }
      }
    }
    $tweets_like_count *= $this->tweet_weightage;
    $tweet_points = (int) $get_tweets['count'] * $this->tweet_point;

    $reply_like_count = 0;
    if (!empty($replies) && is_array($replies)) {
      foreach ($replies as $reply) {
        $reply_likes = get_user_like_status('reply', $reply['tweet_reply_id']);
        if (!empty($reply_likes['total_count'])) {
          $reply_like_count = $reply_like_count + $reply_likes['total_count'];
        }
      }
    }
    $reply_like_count *= $this->tweet_reply_weightage;
    $reply_points = (int) $get_my_replies['count'] * $this->reply_point;

    $album_like_count = 0;
    if (!empty($albums) && is_array($albums)) {
      foreach ($albums as $album) {
        $album_likes = get_user_like_status('album', $album['album_id']);
        if (!empty($album_likes['total_count'])) {
          $album_like_count = $album_like_count + $album_likes['total_count'];
        }
      }
    }
    $album_like_count *= $this->album_weightage;
    $album_points = (int) $get_albums['count'] * $this->album_point;

    $poll_like_count = 0;
    if (!empty($polls) && is_array($polls)) {
      foreach ($polls as $poll) {
        $poll_likes = get_user_like_status('poll', $poll['poll_id']);
        if (!empty($poll_likes['total_count'])) {
          $poll_like_count = $poll_like_count + $poll_likes['total_count'];
        }
      }
    }
    $poll_like_count *= $this->poll_weightage;
    $poll_points = (int) $get_polls['count'] * $this->poll_point;

    $comment_like_count = 0;
    if (!empty($comments) && is_array($comments)) {
      foreach ($comments as $comment) {
        $comment_likes = get_user_like_status('comment', $comment['comment_id']);
        if (!empty($comment_likes['total_count'])) {
          $comment_like_count = $comment_like_count + $comment_likes['total_count'];
        }
      }
    }
    $comment_like_count *= $this->comment_weightage;
    $comment_points = (int) $get_my_comments['count'] * $this->comment_point;

    $photo_like_count = 0;
    if (!empty($photos) && is_array($photos)) {
      foreach ($photos as $photo) {
        $photo_likes = get_user_like_status('photo', $photo['photo_id']);
        if (!empty($photo_likes['total_count'])) {
          $photo_like_count = $photo_like_count + $photo_likes['total_count'];
        }
      }
    }
    $photo_like_count *= $this->photo_weightage;

    $total_points = (int) $tweet_points + $reply_points + $comment_points + $album_points + $poll_points;
    $tweet_like_count = (int) ($tweets_like_count + $reply_like_count);
    $album_like_count = (int) ($album_like_count + $photo_like_count);
    $total_likes_count = (int) ($tweet_like_count + $album_like_count + $poll_like_count + $comment_like_count);
    return (int) $total_likes_count + $total_points;
  }

}

?>